// pages/my/my-index/my-index.js
const controllers=require('../../controllers/ControllerMy').controller
const Controller=require("../../controllers/Controller").controller
const app=getApp()

var startPoint
Page({

  /**
   * 页面的初始数据
   */
  data: {
    globalStype:'',
    myInfo:{},
    role:'',
    roleIcon:'',
    agentImg:'/assets/my/img/agent@2x.png',
    tabbar:[],
    head:'',
    nick:'',
    islogin:false,
    scnum:'',
    pcnum:'',
    ccnum:'',
    phone:'',
    serveState:{
      state2:"",
      state7:"",
      state9:"",
      state8:"",
      state1:"",
    },
    goodsState:{
      state11:"",
      state7:"",
      state8:"",
      state1:""
    },
    cloudState:{},
    clickMe:true,
    icon1:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210916/888088598119710720.png",
    icon2:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210916/888088623193260032.png",
    icon3:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210916/888088646517784576.png",
    buttonRight:0,
    buttonBottom:0
  },


   //以下是按钮拖动事件
   buttonStart: function (e) {
    startPoint = e.touches[0]//获取拖动开始点
  },
  buttonMove: function (e) {
    var endPoint = e.touches[e.touches.length - 1]//获取拖动结束点
    //计算在X轴上拖动的距离和在Y轴上拖动的距离
    var translateX = endPoint.clientX - startPoint.clientX
    var translateY = endPoint.clientY - startPoint.clientY
    startPoint = endPoint//重置开始位置
    var buttonBottom = this.data.buttonBottom - translateY
    var buttonRight = this.data.buttonRight - translateX
    //判断是移动否超出屏幕
    if (buttonRight+50 >= this.data.windowWidth){
      buttonRight = this.data.windowWidth-50;
    }
    if (buttonRight<=0){
      buttonRight=0;
    }
    if (buttonBottom<=0){
      buttonBottom=0
    }
    if (buttonBottom + 50 >= this.data.windowHeight){
      buttonBottom = this.data.windowHeight-50;
    }
    this.setData({
      buttonBottom: buttonBottom,
      buttonRight: buttonRight
    })
  },
  buttonEnd: function (e) {
  },

  toBean(){
    wx.navigateTo({
      url: '/pages/my/my-index/beanPage',
    })
  },

  toCoupon(){
    wx.navigateTo({
      url: '/pages/my/vip/vip_coupon/index',
    })
  },
  toPoint(){
    wx.navigateTo({
      url: '/pages/my/vip/vip_grade/index',
    })
  },
  toAccount(){
    wx.navigateTo({
      url: '/pages/my/vip/vip_ surplus/index',
    })
  },
  toMyCard(e){
    // let type=e.currentTarget.dataset.type
    wx.navigateTo({
      url: '/pages/my/vip/vip_myCard/index',
    })
  },
  toInvitation(){
    // wx.showToast({
    //   title: '暂未开放',
    //   icon:'none'
    // })
    // return
    wx.navigateTo({
      url: '/pages/my/vip/vip_invitation/index',
    })
  },

  //跳转vip中心
  toVipCenter(){
    wx.navigateTo({
      url: '/pages/my/vip/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  //获取用户信息
  tologin(){
    if(wx.getStorageSync('usertoken')){

    }else{
      wx.navigateTo({
        url: '/pages/Login/Login',
      })
    }
  },
  //tabbar跳转
  tabChange(e){
    var index=e.detail.index;
    if(index==0){
      wx.reLaunch({
        url: "/pages/Index/Index"
      });
    }
    if(index==1){
      wx.reLaunch({
        url: "/pages/myVipShop/myVipShop"
      });
    }
    if (index == 2) {
      wx.reLaunch({
        url: "/pages/my/my-friend/my-friend"
      });
    }
    if (index==3){
      wx.reLaunch({
        url: '/pages/my/my-index/my-index',
      })
    }
  },
  //跳转到服务订单页面
  toShopBagCenter(e){
    wx.navigateTo({
      url: '/pages/Mall/Coupon',
    })
  },

  //跳转商圈券
  toShopCircleCoupon(e){
    wx.navigateTo({
      url: '/pages/Index/myShopCircleCoupon',
    })
  },

  //跳转到云商品订单页面
  toCloud(e){
    wx.navigateTo({
      url: '/pages/my/cloudproduct-order/cloudproduct-order?id='+e.currentTarget.id,
    })
  },

  //跳转预约记录
  toRecord(e){
    wx.navigateTo({
      url: '/pages/my/appointment-record/appointment-record',
    })
  },

  //跳转购物车
  toCart(e){
    wx.navigateTo({
      url: '/pages/cart/index/index',
    })
  },

  //退出店铺
  quit(){
    wx.navigateTo({
      url: '/pages/Chain/Chain',
    })
  },

  //跳转到vip中心页面
  toVipCenter(){
    wx.navigateTo({
      url: '/pages/my/vip/index',
    })
  },

  //跳转选择默认地址
  toSelectAddress(){
    wx.navigateTo({
      url: '/pages/my/my-index/selectAddress',
    })
  },
  onLoad: function (options) {
    this.setData({globalStype:app.globalData.globalStype})
    this.setData({
      tabbar: this.data.globalStype=='pinStyle'?app.globalData.tabbar:app.globalData.tabbar2,
    })
    let that=this

    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        // 屏幕宽度、高度
        console.log('height=' + res.windowHeight);
        console.log('width=' + res.windowWidth);
        // 高度,宽度 单位为px
        that.setData({
          windowHeight:  res.windowHeight,
          windowWidth:  res.windowWidth,
          buttonBottom:250,//这里定义按钮的初始位置
          buttonRight:0,//这里定义按钮的初始位置
        })
      }
    })
  },
  onShow: function () {
    if(wx.getStorageSync('phonenumber')){
      this.setData({
        phone:wx.getStorageSync('phonenumber')
      })
    }
    if (wx.getStorageSync('usertoken')) {
        //加载首页数据
        this.getMyInfo()
    }
    //无usertoken，等待login登录回调
    app.usertokenReadyCallback = res => {
      if(res.data.sessionId){
        this.getMyInfo()
      }
    }
  },

 

  getMyInfo(){
    //获取我的信息
    controllers.getMyInfo({
      shopId:wx.getStorageSync('shopId')
    }).then(res =>{
      this.userRole(res.data)
      this.setData({
        myInfo:res.data
      })
      if(res.data){
        this.setData({
          islogin:true
        })
      }
      var that=this
      this.setData({
        'serveState.state2':'',
        'serveState.state7':'',
        'serveState.state9':'',
        'serveState.state8':'',
        'serveState.state1':'',
        'goodsState.state2':'',
        'goodsState.state9':'',
        'goodsState.state8':'',
        'goodsState.state1':'',
        'cloudState.state7':'',
        'cloudState.state11':'',
        'cloudState.state9':'',
        'cloudState.state8':'',
      })
    })

    //获取用户信息
    if(wx.getStorageSync('wxuserInfo')){
      let userinfo=wx.getStorageSync('wxuserInfo')
      this.setData({
        head:userinfo.avatarUrl,
        nick:userinfo.nickName
      })
    }
  },

  // 用户角色
  userRole(info){
    let vip=info.accountRelation.vip
    let role=""
    let roleIcon=""
    switch(vip){
      case 0:
      role='普通用户'
      roleIcon=''
      break;
      case 1:
      role='VIP会员'
      roleIcon='https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20211019/899954587438415872.png'
      break;
      case 2:
      role='普通会员'
      roleIcon='https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20211019/899954478524923904.png'
      break;
      case 3:
      role=info.accountRelation.dispenserDesc||'共享股东'
      roleIcon='https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20211019/899954705621319680.png'
      break;
      case 4:
      role=info.accountRelation.dispenserDesc||'代理商'
      roleIcon='/assets/my/img/agentIcon@2x.png'
      break;
    }
    this.setData({role,roleIcon})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }

  toClickMe(){
    wx.navigateTo({
      url: '/pages/clickMe/clickMe',
    })
  },

  cancleClickMe(){
    this.setData({
      clickMe:false
    })
  },

  //更新用户头像和昵称
  updateUser(){
    console.log("app触发")
    wx.getUserProfile({
      desc:"正在获取",
      success:function(res){
        console.log("getUser触发",res.userInfo)
        let userInfo=res.userInfo
        Controller.updateAccount({
          nick:userInfo.nickName,
          head:userInfo.avatarUrl
        }).then(res=>{
          if(res.code==1){
            wx.showToast({
              icon:"none",
              title: '更新成功',
              duration:1500
            })
          }
        })
      }
    })
  },
})